package com.example.lizhanqi.livevideo.presenter;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.example.lizhanqi.livevideo.bean.LastedBean;
import com.example.lizhanqi.livevideo.model.net.Networking;
import com.example.lizhanqi.livevideo.view.ILastedUI;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Lizhanqi on 2016/9/15 0015.
 */
public class LastedPresenter implements IOnResultPresnter {
    ILastedUI view;
    Networking lastedNet;
    private static final String TAG = "李站旗";
    int Page = 0;
    String userid = "";
    boolean isFresh = false;
    private final LinkedList<LastedBean.ContentBean.ListBean> listBeanLinkedList;
    int totalpage=0;
    private int curpage;
    //实例化的时候传入接口
    public LastedPresenter(ILastedUI view) {
        this.view = view;//传入后用来操作V层
        lastedNet = new Networking(this);
        listBeanLinkedList = new LinkedList<>();
    }
    /*
     *请求网络
     */
    public void requestGET(String url, String type, RequestQueue requestQueue, int what, boolean isFresh,boolean isfirst) {
        this.isFresh = isFresh;
        if (isfirst){
            //请求网络
            String path = url + "page=" + Page + "&type=" + type + "&userid=" + userid;
            lastedNet.requestGET(path, requestQueue, what);
        }else {
            if (totalpage>curpage+1){
                //请求网络
                String path = url + "page=" + curpage+1 + "&type=" + type + "&userid=" + userid;
                lastedNet.requestGET(path, requestQueue, what);
            }

        }

    }
    @Override
    public void ok(Response<String> response,int what) {
        Log.i(TAG, "P网络成功" + response.get());
        if (response.isSucceed()) {
            parseJSON(response.get());
        }
    }
    public void parseJSON(String stringJOSN) {
        Log.i(TAG, "parseJSON: " + stringJOSN);
        if (stringJOSN != null) {
            LastedBean lastedBean = JSONObject.parseObject(stringJOSN, LastedBean.class);
            totalpage=lastedBean.getContent().getPage().getTotalpage();
            curpage = lastedBean.getContent().getPage().getCurpage();
            int state = lastedBean.getState();
            if (state == 0) {
                List<LastedBean.ContentBean.ListBean> list = lastedBean.getContent().getList();
                for (LastedBean.ContentBean.ListBean datas : list) {
                    if (isFresh) {
                        if (!isHave(datas.getRid())) {
                            listBeanLinkedList.addFirst(datas);
                        }
                    } else {
                        if (!isHave(datas.getRid())) {
                            listBeanLinkedList.addLast(datas);
                        }
                    }
                }
                //如果网络请求成功
                view.succeed(listBeanLinkedList);
            }
        }
    }
    public boolean isHave(int Rid) {
        if (listBeanLinkedList.size() != 0) {
            for (LastedBean.ContentBean.ListBean lib : listBeanLinkedList) {
                if (lib.getRid() == Rid) {
                    return  true;
                }
            }
        }
        return false;
    }
    @Override
    public void error(int i, String s, Object o, Exception e, int i1, long l) {
        Log.i(TAG, "error: P网络失败");
        //如果请求网络失败
        view.failed();
    }
}
